package socket.example1;

import java.io.IOException;
import java.io.OutputStream;
import java.net.InetAddress;
import java.net.Socket;

/**
 * @author lhn
 * @date 2024-04-22 11:09
 * Client 发送"hello, server"
 */

public class SocketTCP01Client {
    public static void main(String[] args) throws IOException {
        //连接服务端(ip,port)
        Socket socket = new Socket(InetAddress.getLocalHost(), 9999);
        System.out.println("socket is " + socket.getClass());
        //得到和socket对象得输出流对象
        OutputStream outputStream = socket.getOutputStream();
        //通过输出流，将数据写入数据通道
        outputStream.write("hello, server".getBytes());
        //关闭流和socket的对象，释放占用的资源
        outputStream.close();
        socket.close();
        System.out.println("客户端关闭...");
    }
}
